package com.itfreer.bpm.workflow.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.history.dao.IBpmHistoryDao;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.work.entity.WorkListEntity;

/**
 * 历史数据查询工具
 * @author gj
 *
 */
@Component
public class BpmHistoryQueryUtil {
	/**
	 * 办理过程的数据访问层
	 */
	@Autowired
	protected IBpmHistoryDao historyDao;
	
	/**
	 * 查询待办数据
	 * @param queryFields
	 * @param where
	 * @param order
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	public List<BpmHistoryEntity> getWorkEntitys(String queryFields, Map<String, Object> where, Map<String, Integer> order, Integer pageSize, Integer pageIndex){
		if(where==null) {
			where=new HashMap<String, Object>();
		}
		//historystatus 表示历史状态，0表示历史数据，1表示现状数据，即带办项目，21表示最新数据，也是现状数据,20表示最新的数据，也是历史数据，2表示最新数据，未来的待办数据
		where.put("historystatus:like", "1");
		List<BpmHistoryEntity> resultList = historyDao.getEntitys(queryFields, where, order, pageSize, pageIndex);
		return resultList;
		

	}
	
	/**
	 * 查询已办数据
	 * @param queryFields
	 * @param where
	 * @param order
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	public List<BpmHistoryEntity> getOverWorkEntitys(String queryFields, Map<String, Object> where, Map<String, Integer> order, Integer pageSize, Integer pageIndex){
		if(where==null) {
			where=new HashMap<String, Object>();
		}
		where.put("historystatus:like", "2");
		List<BpmHistoryEntity> resultList = historyDao.getEntitys(queryFields, where, order, pageSize, pageIndex);
		return resultList;
	}
	
	/**
	 * 查询历史数据
	 * @param queryFields
	 * @param where
	 * @param order
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	public List<BpmHistoryEntity> getHistoryEntitys(String queryFields, Map<String, Object> where, Map<String, Integer> order, Integer pageSize, Integer pageIndex){
		if(where==null) {
			where=new HashMap<String, Object>();
		}
		where.put("historystatus:like", "0");
		List<BpmHistoryEntity> resultList = historyDao.getEntitys(queryFields, where, order, pageSize, pageIndex);
		return resultList;
	}
	
	/**
	 * 单纯查询数据
	 * @param queryFields
	 * @param where
	 * @param order
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	public List<BpmHistoryEntity> getEntitys(String queryFields, Map<String, Object> where, Map<String, Integer> order, Integer pageSize, Integer pageIndex){
		return historyDao.getEntitys(queryFields, where, order, pageSize, pageIndex);
	}
	
	/**
	 * 获取用户初始化时操作用户
	 * @param sexeid
	 * @return
	 */
	public BpmHistoryEntity getStartHistory(String sexeid) {
		Map<String, Object> where =new HashMap<>();
		where.put("historystatus:like", "0");
		where.put("taskdefid:=", "U0");
		where.put("sexeid:=", sexeid);
		Map<String, Integer> order =new HashMap<>();
		order.put("opttime", 0);
		List<BpmHistoryEntity> resultList = historyDao.getEntitys(null, where,order , 1, 1);
		if(resultList!=null) {
			return resultList.get(0);
		}else {
			return null;
		}
	
	}
}
